<template>
    <div class="pages">
        <div class="change">
            <div>
                <div>{{ PorgranList.oldPass }}</div>
                <div>
                    <el-input show-password v-model="data.oldPass" placeholder="请输入原密码" width="200"></el-input>
                </div>
            </div>
            <div>
                <div>{{ PorgranList.newPass }}</div>
                <div>
                    <el-input show-password v-model="data.newPass" placeholder="请输入新密码"></el-input>
                </div>
            </div>
            <div>
                <div>{{ PorgranList.confirm }}</div>
                <div>
                    <el-input show-password v-model="data.confirm" placeholder="请再次输入密码"></el-input>
                </div>
            </div>
            <div class="btn">
                <div></div>
                <div>
                    <el-button
                        style="font-size: 16px ; border-radius: 0; border: 1px solid #666 ; color: #666; background-color:#fff ;"
                        @click="clear" type="primary">清空</el-button>
                    <el-button
                        style="font-size: 16px ; border-radius: 0; border: 1px solid #08cfde ; color: #000; background-color:#08cfde ;"
                        @click="add" type="primary">确认</el-button>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { resetPass } from '@/api/user.js'
export default {
    data() {
        return {
            data: {
                oldPass: "",
                newPass: "",
                confirm: ""
            },
            PorgranList: {
                oldPass: "原密码：",
                newPass: "新密码：",
                confirm: "确认密码："
            },
        }
    },
    created() { },
    methods: {
        add() {
            if (this.data.oldPass) {

                if (this.data.newPass) {
                    if (this.isPasswordValid(this.data.newPass)) {
                        if (this.data.confirm) {
                            if (this.data.confirm === this.data.newPass) {
                                resetPass(this.data).then((res) => {
                                    if (res.data.msg) {
                                        this.$message({
                                            message: res.data.msg,
                                            type: 'warning'
                                        });
                                    } else {
                                        this.$message({
                                            message: '修改成功',
                                            type: 'success'
                                        });
                                        localStorage.removeItem('token')
                                        this.$router.push("/")
                                    }

                                }).catch(() => {
                                    this.$message({
                                        message: '网络不给力呦',
                                        type: 'warning'
                                    });
                                })
                            } else {
                                this.$message({
                                    message: '请确认两次输入的密码是否一致',
                                    type: 'warning'
                                });
                            }

                        } else {
                            this.$message({
                                message: '确认密码不能为空',
                                type: 'warning'
                            });
                        }
                    } else {
                        this.$message({
                            message: '新密码必须包含字母、数字、和指定的特殊字符,总长度至少为6个字符',
                            type: 'warning'
                        });
                    }
                } else {
                    this.$message({
                        message: '新密码不能为空',
                        type: 'warning'
                    });
                }
            } else {
                this.$message({
                    message: '原密码不能为空',
                    type: 'warning'
                });
            }
        },
        clear() {
            this.data = {
                oldPass: "",
                newPass: "",
                confirm: ""
            }
        },
        isPasswordValid(password) {
            const passwordRegex = /^[A-Za-z\d@$!%*?&]{6,}$/;

            return passwordRegex.test(password);

        },
    },
}
</script>
<style scoped lang="less">
.pages {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    /* 水平居中 */
    align-items: center;
    /* 垂直居中 */
}

.change {
    // border-radius: 20px;
    padding-left: 20px;
    margin-top: 100px;
    background-color: #fff;
    width: 400px;

    .btn {
        display: flex;
        justify-content: space-between;
        padding-bottom: 50px;
    }
}

.change>div {
    display: flex;
    padding-top: 50px;
    height: 40px;
    width: 380px;
}

.change>div>div:nth-child(1) {
    width: 80px;
    line-height: 40px;
    text-align: right;
}

.change>div>div:nth-child(2) {
    width: 300px;
}

.change>.btn>div:nth-child(2) {
    width: 160px;
}
</style>